/**
 * EditorActions.java
 *
 * Copyright (C) 2009,  Tomislav Milkovic
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package extjsdyntran.servlets.actions;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import ws4is.engine.utils.LoggerFactory;

import extjsdyntran.translation.ServiceTypes;
import extjsdyntran.translation.TranslationFactory;
import extjsdyntran.translation.ITranslationService;

import ws4is.extjs.grid.AGridActions;
import extjsdyntran.servlets.Constants;

public final class EditorActions extends AGridActions {


	private static final long serialVersionUID = 1L;

	final static Logger logger = LoggerFactory.get();
	
	private String language = null;
	
	//for translation editor only; if TRUE; new language is saved as proxy translation service 
	private boolean proxyGroup = false;

	/******************************************
	 * POJO Methods *
	 ******************************************/
	public void setLanguage(final String language) {
		this.language = language;
	}

	public void setProxyGroup(final boolean proxyGroup) {
		this.proxyGroup = proxyGroup;
	}
	/******************************************
	 * HTTP-GET Methods 					  *
	 * used for grid commands;toolbar...etc   *
	 ******************************************/

	// remove language from cache
	// http://.../servlet?action=EditorActions&cmd=remove
	public void remove(HttpServletRequest request,	HttpServletResponse response) throws ServletException, IOException {
		if(logger.isDebugEnabled()){
			logger.debug("Remove method called...");
		}
		setJsonResponseHeader(response); 
		PrintWriter pw = getPrintWriter(response);
		if (language != null) {
			ITranslationService service = TranslationFactory.getRealService(language);
			if (!service.removeLanguageFromCache(language)) {
				pw.print(getJSONResponse(false, Constants.err8));
			} else
				pw.print(getJSONResponse(true, null));
		} else {
			pw.print(getJSONResponse(false, Constants.err1));
		}

	}

	// add new language to cache
	// http://.../servlet?action=EditorActions&cmd=add&proxyGroup=TRUE/FALSE
	public void add(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		if(logger.isDebugEnabled()){
			logger.debug("Add method called...");
		}
		setJsonResponseHeader(response);
		PrintWriter pw = getPrintWriter(response);

		if (language == null) {
			pw.print(getJSONResponse(false, Constants.err1));
			return;
		}


		if (language.length() < 3) {
			pw.print(getJSONResponse(false, Constants.err13));
			return;
		}

		ITranslationService transervice = null;
		if (proxyGroup){
			transervice = TranslationFactory.getEditableService(language,ServiceTypes.PROXY);
		} else transervice = TranslationFactory.getEditableService(language,ServiceTypes.NORMAL);
		
		
		if (transervice == null) {
			pw.print(getJSONResponse(false, Constants.err12));
		} else if (!transervice.appendLanguageToCache(language)) {
			pw.print(getJSONResponse(false, Constants.err9));
		} else
			pw.print(getJSONResponse(true, null));

	}

	/******************************************
	 * HTTP-POST Methods 					  *
	 * ExtJs grid record commands		      *
	 ******************************************/

	public void create(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(logger.isDebugEnabled()){
			logger.debug("Create method called...");
		}
		setJsonResponseHeader(response); 
		PrintWriter pw = getPrintWriter(response);
		ITranslationService service = TranslationFactory.getRealService(language);
		String r = EditorRecordManager.createRecords(service, this.language,this.data);
		pw.write(r);
	}

	public void read(HttpServletRequest request, HttpServletResponse response)	throws ServletException, IOException {
		if(logger.isDebugEnabled()){
			logger.debug("Read method called...");
		}
		setJsonResponseHeader(response);
		PrintWriter pw = getPrintWriter(response);
		ITranslationService service = TranslationFactory.getRealService(language);
		String r = EditorRecordManager.getTranslationRecords(service,this.language);
		pw.write(r);
	} 

	public void destroy(HttpServletRequest request,	HttpServletResponse response) throws ServletException, IOException {
		if(logger.isDebugEnabled()){
			logger.debug("Destroy method called...");
		}
		setJsonResponseHeader(response); 
		
		PrintWriter pw = getPrintWriter(response);
		ITranslationService service = TranslationFactory.getRealService(language);
		String r = EditorRecordManager.deleteRecords(service, this.language,this.data);
		pw.write(r);
	}

	public void update(HttpServletRequest request,	HttpServletResponse response) throws ServletException, IOException {
		if(logger.isDebugEnabled()){
			logger.debug("Update method called...");
		}
		setJsonResponseHeader(response);
		PrintWriter pw = getPrintWriter(response);
		ITranslationService service = TranslationFactory.getRealService(language);
		String r = EditorRecordManager.updateRecords(service, this.language,this.data);
		pw.write(r);		
	}


	public boolean defaultReponse(String cmd, HttpServletRequest request,HttpServletResponse response) throws Exception {
		return false;
	}


}
